package com.avcdata.aoscustommade.activity;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.avcdata.aoscustommade.R;
import com.avcdata.aoscustommade.bean.DateSelectCategoryBean;
import com.avcdata.aoscustommade.main.BaseActivity;
import com.avcdata.aoscustommade.model.CityDatailActivityModel;
import com.avcdata.aoscustommade.model.WelcomeViewModel;
import com.avcdata.aoscustommade.presenter.WelcomePresenter;
import com.avcdata.aoscustommade.utils.AppActivityManager;
import com.avcdata.aoscustommade.utils.AppUtils;
import com.avcdata.aoscustommade.utils.AvcConstants;
import com.avcdata.aoscustommade.utils.SharedPreferencesUtils;
import com.avcdata.aoscustommade.utils.TextUitl;
import com.avcdata.aoscustommade.utils.http.AvcHttpUtils;
import com.avcdata.aoscustommade.utils.http.AvcJsonHttpResponseHandler;
import com.avcdata.aoscustommade.utils.http.AvcRequestParams;
import com.avcdata.aoscustommade.view.WelcomeView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;


public class Welcome_Activity extends BaseActivity implements WelcomeView,ActivityCompat.OnRequestPermissionsResultCallback{


    private SharedPreferencesUtils sp;
    private WelcomePresenter welcomePresenter;

    @Override
    public void initView() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_welcome);
        sp = new SharedPreferencesUtils(this);
        AppActivityManager.getAppManager().addActivity(this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 10010:
                if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
                    // TODO: 2017/8/17 为适应6.0及以上版本，修改为动态申请权限
                    sp.setKey(AppUtils.getIMEI(Welcome_Activity.this));
                    loadGetDateSelectCategory();
                }else{
                    Log.e("","权限获取失败");
                }
                break;

            default:
                Log.e("","权限获取失败");
                break;
        }
    }

    @Override
    public void initInstall() {
        showSplash();

        welcomePresenter = new WelcomePresenter(this, new WelcomeViewModel(Welcome_Activity.this));

        int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE);

        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE},10010);
        } else {
            //TODO
            sp.setKey(AppUtils.getIMEI(Welcome_Activity.this));
            loadGetDateSelectCategory();
        }

    }

    @Override
    public void initData() {

    }

    @Override
    public void evenControl() {

    }

    @Override
    public void onClickEvent(View view) {

    }


    private void showSplash() {
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Intent intent = new Intent(Welcome_Activity.this, Login_Activity.class);
                startActivity(intent);
                finish();
            }
        }.start();

    }

    public void showSplashNot() {
        new Thread() {
            public void run() {
                try {
                    Thread.sleep(2000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Intent intent = new Intent();
                intent.putExtra("from", "main");
                intent.setClass(Welcome_Activity.this, Welcome_Activity.class);
                startActivity(intent);
                finish();
            }
        }.start();
    }


    /**
     * 获取当前周
     */
    private void loadGetDateSelectCategory() {
        welcomePresenter.loadGetDateSelectCategory("0");
    }

    //获取当前周成功
    @Override
    public void loadGetDateSelectCategorySuccess(JSONObject jsonObject) {
        try {
            if (!TextUitl.isEmpty(jsonObject.toString())) {
                boolean success = jsonObject.getBoolean("success");
                if (success && jsonObject.has("returnValue")) {
                    String returnValue = jsonObject.getString("returnValue").trim();
                    if (!TextUitl.isEmpty(returnValue)) {
                        JSONArray returnValueArray = new JSONArray(returnValue);
                        if (returnValueArray.length() != 0) {
                            sp.setNewWeek(returnValueArray.get(0).toString());
                        }
                    }

                }
                Log.e("获取最新周",""+jsonObject.toString());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
